package controllers;

import models.Event;
import models.Presentation;
import play.mvc.Controller;
import util.JsonResponse;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: bicho
 * Date: 29/01/12
 * Time: 17:19
 * To change this template use File | Settings | File Templates.
 */
//@With(Secure.class)

public class ManageEventPresentationsController extends Controller {

    public static void edit(String nick) {

    }

    public static void list(String nick) {
//        if (!Security.check(Task.GESTION_PONENCIAS + "|" + nick)) {
//            forbidden();
//        } else {
        Event event = Event.find("byNick", nick).first();
//            List<Turn> turns = Turn.find("byEvent", event).fetch();
//        EventPageController.showPresentations(nick);
//            render(event, turns);
        render(event);
//        }
    }

    public static void show(String nick) {
//        if (!Security.check(Task.GESTION_PONENCIAS + "|" + nick)) {
//            forbidden();
//        } else {
        Event.reloadByNick(nick);
        Event event = Event.getByNick(nick);
        event = Event.findById(event.id);
        render(event);
//        }
    }


    public static void save(String nick, String presentationId, String start, String end, String title, String description) {
        try {
            Date startDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(start).toGregorianCalendar().getTime();
            Date endDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(end).toGregorianCalendar().getTime();
            Event event = Event.getByNick(nick);

            Presentation presentation;
            long l = Long.parseLong(presentationId);
            if (l == 0) {
                presentation = new Presentation();
            } else {
                presentation = Presentation.findById(l);
            }
            presentation.event = event;
            presentation.name = title;
            presentation.description = description;
            presentation.dateBegin = startDate;
            presentation.dateEnd = endDate;
            presentation.save();

        } catch (DatatypeConfigurationException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            renderJSON(new JsonResponse(e.getMessage(), false));
        }
        renderJSON(new JsonResponse("Realizado satisfactoriamente!", true));
    }

    public static void delete(String nick, String presentationId) {
        Presentation presentation;
        long l = Long.parseLong(presentationId);
        if (l == 0) {
            renderJSON(new JsonResponse("No se puede encontrar una presentacion con el identificador proporcionado", false));
        } else {
            presentation = Presentation.findById(l);
            presentation.delete();
            renderJSON(new JsonResponse("Realizado satisfactoriamente!", true));
        }

    }

}
